Inport data

results <- read.csv("results.csv") %>% 
  mutate(RR=exp(Value),
         "95%CI" =paste0(format(round( exp(Value -1.96*Std.Error), digits = 3),nsmall=3)," - " ,
                                    format(round(exp( Value +1.96*Std.Error), digits = 3),nsmall=3))) %>% 
  mutate(SA3_CODE16=as.character(SA3_CODE16)) %>% 
  mutate(RR=round(RR,2),
         IRSAD=round(IRSAD,0)) %>% 
  mutate(Population=factor(Population,levels=c("Age 12-17", "Age 18-25", "Females",  "Males", "All young people"))) %>% 
  mutate(Sum= paste0(format(RR, nsmall=2),  " 95% CI:",`95%CI`)) %>% 
  mutate(RR=ifelse(IRSAD <=810,NA,RR),
         Sum=ifelse(IRSAD <=810,NA,Sum))

dta <- readRDS( here("COVID","COVID_cleaned.rds"))
map <- readRDS(here("Data", "Shapefile_SA3.rds"))

map@data <- map@data %>%
  left_join(results %>% filter( Population == "All young people" ) , by = "SA3_CODE16") 
map <- sf::st_as_sf(map)

Plot

dta<-results %>% 
  #3 sa3 excluded at the tail
  dplyr::filter( IRSAD > 810)

dta <- highlight_key(dta, ~SA3_NAME)
plot<-  ggplot(dta, aes(x=IRSAD, y=RR,fill="")) +
  geom_point( aes(text=paste0(SA3_NAME,"\nRR: ",format(RR, nsmall=2),
                              " 95% CI:",`95%CI`),fill="#0072B2"),
              alpha=0.6, col="#0072B2", size=1)+
  geom_smooth(col="red",span=1,se=FALSE) +
  facet_wrap( ~ Population,ncol = 5)+ 
  theme_bw()  +
  theme(legend.position = "none",
        strip.background=element_rect(fill="white"))
## Warning: Ignoring unknown aesthetics: text
bs_gg2 <- ggplotly(plot,tooltip="text",
                    height = 400, 
                    width = 800) 
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
highlight(bs_gg2)
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

Map

bins <- c(0.6,0.7, 0.8, 0.9,1.0 ,1.1, 1.2, 1.3,1.4, Inf)
pal <- leaflet::colorBin("YlGnBu", domain = map$RR, bins = bins)

labels <- sprintf(
  "<strong>%s</strong><br/> RR: %s",
  map$SA3_NAME16, map$Sum
) %>% lapply(htmltools::HTML)

sd <- highlight_key(map, ~SA3_NAME16 )


map1<- leaflet(sd) %>%
  addPolygons(color = "#444444",
              weight = 1, 
              smoothFactor = 0.5,
    opacity = 1.0, fillOpacity = 0.5,
    fillColor = ~pal(RR),
    label = labels,
    highlightOptions = highlightOptions(
      color = "white", weight = 2,
      bringToFront = TRUE))
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +no_defs).
## Need '+proj=longlat +datum=WGS84'
plot<-  ggplotly(ggplot(sd, aes(x=IRSAD, y=RR)) +
    geom_point( aes(text=paste0(SA3_NAME,"\nRR",format(RR, nsmall=2),
                              " 95% CI:",`95%CI`)),
              alpha=0.6, col="#0072B2", size=1)+
    geom_smooth(col="red",span=1,se=FALSE) +
    xlim(840,1180)+
    theme_bw()  +
    theme( strip.background=element_rect(fill="white")),
  height = 400,  width = 400)  
## Warning: Ignoring unknown aesthetics: text
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 8 rows containing non-finite values (stat_smooth).
withr::with_options(
  list(persistent = TRUE), 
  crosstalk::bscols(widths = c(6, 6),map1, plot)
)